package com.eric.windows.event;

import android.app.ExpandableListActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.eric.windows.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;

@EActivity(R.layout.activity_my_expand_list)
public class MyExpandListActivity extends ExpandableListActivity {

    private String[] armTypes = {"神族","人族","虫族"};
    private String[] arms = {"海军","空军","陆军"};

    ExpandableListAdapter adapter  = new BaseExpandableListAdapter() {
        @Override
        public int getGroupCount() {
            return armTypes.length;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return arms.length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return armTypes[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return arms[childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        /**
         * Indicates whether the child and group IDs are stable across changes to the
         * underlying data.
         *
         * @return whether or not the same ID always refers to the same object
         * @see Adapter#hasStableIds()
         */
        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            ItemGrouMyExList grouMyExList = null;
            GroupHolder holder = null;
            if (convertView == null){
//                convertView =  LayoutInflater.from(MyExpandListActivity.this).inflate(R.layout.item_group_melist,parent,false);
//                holder = new GroupHolder();
//                holder.txt = (TextView) convertView.findViewById(R.id.time_group_txt_meList);
//                convertView.setTag(holder);
                grouMyExList = ItemGrouMyExList_.build(MyExpandListActivity.this);

                convertView = grouMyExList;

            }else {
//                holder = (GroupHolder) convertView.getTag();
                grouMyExList = (ItemGrouMyExList) convertView;
            }

//            holder.txt.setText(armTypes[groupPosition]);
            grouMyExList.update(armTypes[groupPosition]);
            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            ItemViewMyExList viewMyExList = null;

            ViewHolder holder = null;
            if (convertView == null){
//                convertView =  LayoutInflater.from(MyExpandListActivity.this).inflate(R.layout.item_txt_melist,parent,false);
//                holder = new ViewHolder();
//                holder.txt = (TextView) convertView.findViewById(R.id.item_txt_meList);
//                convertView.setTag(holder);

                viewMyExList =  ItemViewMyExList_.build(MyExpandListActivity.this);

                convertView = viewMyExList;
            }else {
//                holder = (ViewHolder) convertView.getTag();
                viewMyExList = (ItemViewMyExList) convertView;
            }
//            holder.txt.setText(arms[childPosition]);

            viewMyExList.update(arms[childPosition]);

            return convertView;
        }

        /**
         * Whether the child at the specified position is selectable.
         *
         * @param groupPosition the position of the group that contains the child
         * @param childPosition the position of the child within the group
         * @return whether the child is selectable.
         */
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    };



    @AfterViews
    void initViews(){
        setListAdapter(adapter);
        getExpandableListView().setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(MyExpandListActivity.this, (armTypes[groupPosition] + arms[childPosition]), Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        getExpandableListView().setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

                return false;
            }
        });
    }

    static class GroupHolder{
        TextView txt;
    }

    static class ViewHolder{
        TextView txt;
    }


}
